package com.ochnas.volunteersignup.ui;

import static com.ochnas.volunteersignup.ui.SharedConstants.SORT_BY_ASCENDING_DATE;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ochnas.volunteersignup.domain.Event;
import com.ochnas.volunteersignup.service.VolunteerService;
public class HomeController {
	private ApplicationService applicationService;
	private VolunteerService volunteerService;
	private List<Event> events;
	
	private int size;
	
	public HomeController() {
		super();
		events = new ArrayList<Event>();
		
	}
	
	private void refreshHome() {
		Date startDate = new Date(System.currentTimeMillis());
		List<Event> myEvents = volunteerService.findAllFutureEvents(startDate);
		Event[] eventArray = myEvents.toArray(new Event[0]);
		Arrays.sort(eventArray, SORT_BY_ASCENDING_DATE);
		events.clear();
		events.addAll(Arrays.asList(eventArray));
		size = events.size();
	}
	
	public String home() {
		refreshHome();
		return "home";
	}
	
	public ApplicationService getApplicationService() {
		return applicationService;
	}
	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}
	
	public VolunteerService getVolunteerService() {
		return volunteerService;
	}

	public void setVolunteerService(VolunteerService volunteerService) {
		this.volunteerService = volunteerService;
	}

	public List<Event> getEvents() {
		refreshHome();
		return events;
	}
	public void setEvents(List<Event> events) {
		this.events = events;
	}

	public int getSize() {
		refreshHome();
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}
	
}
